package com.piz.service.productservice;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import com.piz.dao.product.ProductCategoryDao;
import com.piz.dao.product.ProductClassificationDao;
import com.piz.dao.product.ProductDao;
import com.piz.dao.product.ProductServeDao;
import com.piz.dao.product.TasteDao;
import com.piz.model.ItemCategory;
import com.piz.model.ItemClassification;
import com.piz.model.ItemDetails;
import com.piz.model.ItemPricing;
import com.piz.model.ItemTaste;
import com.piz.model.Serve;
import com.piz.service.common.UnexpectedServiceException;





public class ProductService {
	
	@Autowired
	private ProductServeDao serveDao;
	
	@Autowired
	private ProductCategoryDao categoryDao;
	
	@Autowired
	private ProductClassificationDao classificationDao;
	
	@Autowired
	private TasteDao tasteDao;
	
	@Autowired
	private ProductDao productDao;
	
	@SuppressWarnings("unchecked")
	public void addProduct(ItemDetails itemDetails) throws UnexpectedServiceException
	{
		Map<Integer, String> catMap = fetchProductCategoryMap();
		Map<Integer, String> typeMap = fetchProductTypeMap();
		
		String catName = catMap.get(itemDetails.getClassificationId().intValue());
		String type = typeMap.get(itemDetails.getCategoryId().intValue());
		
	 if(itemDetails.getSinglePrice() != null)
	 {
		 List<ItemPricing> prices = new ArrayList<ItemPricing>();
		 ItemPricing singlePrice = new ItemPricing();
		 singlePrice.setPrice(itemDetails.getSinglePrice());
		 singlePrice.setServe(serveDao.search(Serve.class, -1l));
		 singlePrice.setItemDetails(itemDetails);
		 prices.add(singlePrice);
		 itemDetails.setPrices(prices);
	 }
	 else
	 {
		 List<ItemPricing> prices  = itemDetails.getPrices();
		 long i =1;
		 for(ItemPricing itemPricing : prices)
		 {
			 if(itemPricing.getServe() == null)
			 {
				 itemPricing.setServe(serveDao.search(Serve.class, i));
			  }
			 i++;
		 }
		 itemDetails.setPrices(prices);
	 }
	 String fileName = "";
	 try {
		 if(itemDetails.getFile() != null)
			 fileName = "images1/"+catName+"/"+type+"/"+itemDetails.getName();
	} catch (Exception e) {
		throw new UnexpectedServiceException("Exception in creating file path "+fileName, e);
	}
	itemDetails.setImage(fileName);
	productDao.saveOrUpdate(itemDetails);
}
	
	public void saveProductImage(String realPath,ItemDetails itemDetails) throws UnexpectedServiceException
	{
		Map<Integer, String> catMap = fetchProductCategoryMap();
		Map<Integer, String> typeMap = fetchProductTypeMap();
		
		String catName = catMap.get(itemDetails.getClassificationId().intValue());
		String type = typeMap.get(itemDetails.getCategoryId().intValue());
		
		MultipartFile multipartFile = itemDetails.getFile();
		 String fileName="";
		// image type of file processing...
		 System.err.println("-------------------------------------------");
		 try {
		   InputStream inputStream = null;
		   OutputStream outputStream = null;
		   if (multipartFile.getSize() > 0) {
			  inputStream = multipartFile.getInputStream();
			  fileName = realPath + "/images1/"+catName+"/"+type;
			  File theDir = new File(fileName);
			  if (!theDir.exists())
			  {
			    System.out.println("creating directory: " + fileName);
			    boolean result = theDir.mkdirs();  
			    if(result){    
			       System.out.println("DIR created");  
			     }

			  }
			  fileName = fileName+"/"+itemDetails.getName();
			  outputStream = new FileOutputStream(fileName);
			  int readBytes = 0;
			  byte[] buffer = new byte[10000];
			  while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {
				 outputStream.write(buffer, 0, readBytes);
			   }
			  outputStream.close();
			  inputStream.close();
			}	    
		  } catch (Exception e) {
			 throw new UnexpectedServiceException("Exception in saving image file: "+fileName,e);
		  }
		 // .......................................... 
	}
	
	public List<Serve> getAllProductServes() throws UnexpectedServiceException
	{
		return serveDao.getAllServes();
	}
	
	public List<ItemCategory> getProductTypes() throws UnexpectedServiceException
	{
		return categoryDao.getAllProductTypes();
	}
	
	public List<ItemDetails> getAllToppings() throws UnexpectedServiceException
	{
		return productDao.findAllToppings();
	}
	
	public List<ItemDetails> getVegToppings() throws UnexpectedServiceException
	{
		Map<Integer, String> productCategoryMap = fetchProductCategoryMap();
		List<ItemDetails> topps= getAllToppings();
		List<ItemDetails> vegTopps = new ArrayList<ItemDetails>();
		for(ItemDetails topp : topps)
		{
			String className = productCategoryMap.get(topp.getClassificationId().intValue());
			if(className.contains("veg") || className.contains("VEG"))
			{
				vegTopps.add(topp);
			}
		}
		return vegTopps;
	}
	
	public List<ItemDetails> getNonVegToppings() throws UnexpectedServiceException
	{
		Map<Integer, String> productCategoryMap = fetchProductCategoryMap();
		List<ItemDetails> topps= getAllToppings();
		List<ItemDetails> nonVegTopps = new ArrayList<ItemDetails>();
		for(ItemDetails topp : topps)
		{
			String className = productCategoryMap.get(topp.getClassificationId().intValue());
			if(className.contains("non veg") || className.contains("NON VEG"))
			{
				nonVegTopps.add(topp);
			}
		}
		return nonVegTopps;
	}
	
	public Map<Integer, String> fetchProductTypeMap() throws UnexpectedServiceException
	{
		List<ItemCategory> prdTypes = getProductTypes();
		Map<Integer, String> types =  new HashMap<Integer, String>();
		for(ItemCategory productType : prdTypes)
		{
			types.put(productType.getId().intValue(), productType.getName());
		}
		return types;
	}
	
	
	
	public List<ItemClassification> getAllCategories() throws UnexpectedServiceException
	{
		return classificationDao.getAllItemCategories();
	}
	
	public Map<Integer, String> fetchProductCategoryMap() throws UnexpectedServiceException
	{
		List<ItemClassification> itemCategories = getAllCategories();
		Map<Integer, String> cat =  new HashMap<Integer, String>();
		for(ItemClassification category : itemCategories)
		{
			cat.put(category.getId().intValue(), category.getName());
		}
		return cat;
	}
	
	public List<ItemTaste> getAllTastes() throws UnexpectedServiceException
	{
		return tasteDao.getAllTastes();
	}

	public Map<Integer, String> fetchPizzaTasteMap() throws UnexpectedServiceException
	{
		List<ItemTaste> tastes = getAllTastes();
		Map<Integer, String> tasteMap =  new HashMap<Integer, String>();
		for(ItemTaste taste : tastes)
		{
			tasteMap.put(taste.getId().intValue(), taste.getTname());
		}
		return tasteMap;
	}
	
	public ItemDetails fetchPizzaById(Long id) {
		return productDao.findPizzaById(id);
	}
	
	public ProductServeDao getServeDao() {
		return serveDao;
	}

	public void setServeDao(ProductServeDao serveDao) {
		this.serveDao = serveDao;
	}

	public ProductCategoryDao getCategoryDao() {
		return categoryDao;
	}

	public void setCategoryDao(ProductCategoryDao categoryDao) {
		this.categoryDao = categoryDao;
	}

	public ProductClassificationDao getClassificationDao() {
		return classificationDao;
	}

	public void setClassificationDao(ProductClassificationDao classificationDao) {
		this.classificationDao = classificationDao;
	}

	public TasteDao getTasteDao() {
		return tasteDao;
	}

	public void setTasteDao(TasteDao tasteDao) {
		this.tasteDao = tasteDao;
	}

	public ProductDao getProductDao() {
		return productDao;
	}

	public void setProductDao(ProductDao productDao) {
		this.productDao = productDao;
	}

}
